import { Component, OnInit } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { ScheduleSaveDialogComponent } from '../schedule-save/schedule-save.component';
import { ScheduleService } from '../service/schedule.service';
import { Schedule } from '../model/schedule';

@Component({
  selector: 'app-schedule-menu',
  templateUrl: './schedule-menu.component.html',
  styleUrls: ['./schedule-menu.component.scss']
})
export class ScheduleMenuComponent implements OnInit {

  constructor(public dialog: MatDialog, public scheduleService: ScheduleService) { }

  ngOnInit() {
  }

  async openSaveScheduleDialog() { 
    try {
      let dialogRef = this.dialog.open(ScheduleSaveDialogComponent, {
        width: '500',
        data: {
        }
      });

      let schedule: Schedule = await dialogRef.afterClosed().toPromise();
      if (!schedule) { 
        return;
      }
      if (schedule.id) {
        schedule = await this.scheduleService.update(schedule as Schedule, {});
      } else { 
        schedule = await this.scheduleService.add(schedule as Schedule, {});
      }
      console.log(schedule);
    } catch (err) { 
      console.error(err);
      if (err.msg) { 
        return alert(err.msg);
      }
      alert("系统错误")
    }
  }
}
